package com.hrm.service;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hrm.dto.Response;
import com.hrm.dto.ResponseDTO;
import com.hrm.entity.Room;
import com.hrm.mapper.RoomMapper;
import com.hrm.vo.RoomVO;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 请假表 服务类
 * </p>
 *
 * @author qiujie
 * @since 2022-03-27
 */
@Service
public class RoomService extends ServiceImpl<RoomMapper, Room> {

    @Resource
    private RoomMapper roomMapper;

    public ResponseDTO add(Room room) {
        if (save(room)) {
            return Response.success();
        }
        return Response.error();
    }

    public ResponseDTO deleteById(Integer id) {
        if (removeById(id)) {
            return Response.success();
        }
        return Response.error();
    }

    @Transactional(rollbackFor = Exception.class)
    public ResponseDTO deleteBatch(List<Integer> ids) {
        if (removeBatchByIds(ids)) {
            return Response.success();
        }
        return Response.error();
    }

    public ResponseDTO edit(Room room) {
        if (updateById(room)) {
            return Response.success();
        }
        return Response.error();
    }

    public ResponseDTO list(Integer current, Integer size) {
        IPage<RoomVO> config = new Page<>(current, size);
//        if (name == null) {
//            name = "";
//        }
        IPage<RoomVO> page = this.roomMapper.listAttTrainVO(config);
        // 将响应数据填充到map中
        Map map = new HashMap();
        map.put("pages", page.getPages());
        map.put("total", page.getTotal());
        map.put("list", page.getRecords());
        return Response.success(map);
    }

    public ResponseDTO findById(Integer id) {
        Room room = getById(id);
        if (room != null) {
            return Response.success(room);
        }
        return Response.error();
    }

    public ResponseDTO findByStaffId(Integer current, Integer size, Integer id) {
        IPage<RoomVO> config = new Page<>(current, size);
        IPage<RoomVO> page = this.roomMapper.listAttTrainVOByStaffId(config, id);
        // 将响应数据填充到map中
        Map map = new HashMap();
        map.put("pages", page.getPages());
        map.put("total", page.getTotal());
        map.put("list", page.getRecords());
        return Response.success(map);
    }
}




